const path = require("path");
const webpack = require("webpack");
const Merge = require("webpack-merge");
const CommonConfig = require("./webpack.common.config.js");
const HtmlwebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
// 定义了一些文件夹的路径
const ROOT_PATH = path.resolve(__dirname);
const APP_PATH = path.resolve(ROOT_PATH, 'src');
const BUILD_PATH = path.resolve(ROOT_PATH, 'dist');
module.exports = Merge(CommonConfig, {
    entry: {
        personal: path.resolve(APP_PATH, 'personal/main.js'),
        vendor: ['jquery', 'vue', 'babel-polyfill']
    },

    output: {
        path: BUILD_PATH,
        publicPath: '/Editor/',
        filename: 'js/[name]_[chunkhash:8].js'
    },

    module: {
        rules: [
            // 加载图片并置于images文件夹下
            {
                test: /\.(png|jpg|jpeg|gif)(\?.+)?$/,
                loader: 'url-loader?limit=8192&name=/images/[name]_[hash:8].[ext]'
            },
            // 加载字体并置于fonts文件夹下
            {
                test: /\.(eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
                loader: 'url-loader?limit=8192&name=/fonts/[name]_[hash:8].[ext]'
            }
        ]
    },

    plugins: [
        new CleanWebpackPlugin(),
        // 单人
        new HtmlwebpackPlugin({
            template: path.resolve(APP_PATH, 'personal/index.html'),
            filename: 'index.html',
            chunks: ['runtime', 'vendor', 'personal'],
            //要把script插入到标签里
            inject: 'body',
        }),
        // 压缩代码
        new UglifyJSPlugin(),
        new webpack.HashedModuleIdsPlugin(),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor'
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'runtime'
        }),
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: JSON.stringify('production')
            }
        }),
        new CopyWebpackPlugin(
            [
                {from: 'ueditor', to: 'ueditor'},
                {from: 'copy_assets/images', to: 'images'},
                {from: 'copy_assets/js', to: 'js'},
                {from: 'tencentlib', to: 'tencentlib'},
                {from: 'layui', to: 'layui'}
            ]
            , {
                debug: true
            }
        )
    ]
})
